package org.example.config;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.UserDetailsManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：zj@uinpay.cn
 * @Date: 2024/8/24
 * @Desc:
 */
public class DbUserDetailsManage implements UserDetailsManager, UserDetailsPasswordService {
    @Override
    public UserDetails updatePassword(UserDetails user, String newPassword) {
        return null;
    }

    @Override
    public void createUser(UserDetails user) {

    }

    @Override
    public void updateUser(UserDetails user) {

    }

    @Override
    public void deleteUser(String username) {

    }

    @Override
    public void changePassword(String oldPassword, String newPassword) {

    }

    @Override
    public boolean userExists(String username) {
        return false;
    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 根据用户名获取用户信息

        // 用户的权限信息
        List<GrantedAuthority> list = new ArrayList<>();
        list.add(new GrantedAuthority() {
            @Override
            public String getAuthority() {
                return "user_list";
            }
        });
/*        return new User("zhangsan",
                "112233",
                true,
                true,
                true,
                true,
                list);*/
        // 用户角色授权

        PasswordEncoder encoder = new BCryptPasswordEncoder();
        String encode = encoder.encode("123");

        return User.withUsername("zhangsan").password(encode)
                .roles("admin")
                .authorities("role1", "role2")//多个权限  后面的会覆盖前面的
                .build();
    }
}
